﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Wechat
{
    /// <summary>
    /// 公众平台拉取用户信息
    /// </summary>
    public class MediaPlatformUserInfoGet
    {
        private readonly string appid, secret;
        /// <summary>
        /// 公众平台拉取用户信息
        /// </summary>
        /// <param name="app_id"></param>
        /// <param name="app_secret"></param>
        public MediaPlatformUserInfoGet(string app_id, string app_secret)
        {
            this.appid = app_id;
            this.secret = app_secret;
        }
        /// <summary>
        /// 用户必须关注公众号才能获取到信息
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public WechatUserInfo GetUserInfo(string openid)
        {
            var mpa = new MediaPlatformAuthentication(this.appid, this.secret);
            string token = mpa.GetAccessToken();
            string interfaceUrl = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", token, openid);
            return HttpHelper.Get<WechatUserInfo>(interfaceUrl);
        }
    }
    public class WechatUserInfo
    {
        #region Properties
        /// <summary>
        /// 是否关注公众号，非常重要。如subscribe=0未关注则无法拉取信息
        /// </summary>
        public int Subscribe { get; set; }
        /// <summary>
        /// 用户在公众号的ID
        /// </summary>
        public string OpenID { get; set; }
        /// <summary>
        /// 用户昵称
        /// </summary>
        public string NickName { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        [JsonProperty("sex")]
        public int Gender { get; set; }
        /// <summary>
        /// 语种
        /// </summary>
        public string Language { get; set; }
        /// <summary>
        /// 所在城市
        /// </summary>
        public string City { get; set; }
        /// <summary>
        /// 所在省份
        /// </summary>
        public string Province { get; set; }
        /// <summary>
        /// 所在国家
        /// </summary>
        public string Country { get; set; }
        /// <summary>
        /// 头像
        /// </summary>
        public string HeadImgUrl { get; set; }
        /// <summary>
        /// 关注公众号的时间，时间戳
        /// </summary>
        [JsonProperty("subscribe_time")]
        public int SubscribeTime { get; set; }
        /// <summary>
        /// 开放平台unionid
        /// </summary>
        public string UnionID { get; set; }
        /// <summary>
        /// 备注信息
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// 用户分组ID
        /// </summary>
        public int GroupId { get; set; }
        /// <summary>
        /// 用户的标签集合
        /// </summary>
        [JsonProperty("tagid_list")]
        public int[] TagIDList { get; set; }
        #endregion
    }
}
